package com.yy.ant.chat.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.yy.ant.sys.service.IUserService;
import com.yy.model.ResultMsg;
import com.yy.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yy.ant.sys.entity.User;
import com.yy.utils.SessionUtil;

@RestController
@RequestMapping("/chat/uploadModule")
public class UploadModule {

	@Value("${yy.chat}")
	private String chat;
	@Value("${yy.chat.url}")
	private String chat_url;
	@Autowired
	private IUserService iUserService;
	
	/**
	 * 发送图片,上传图片接口
	 */
	@RequestMapping(value = "/image")
	public Object image(@RequestHeader("token") String token,@RequestParam(value = "file") MultipartFile file) {
		ResultMsg resultMsg1= JwtUtils.validateJWT(token);
		if (!resultMsg1.isSuccess()){
			return null;
		}
		Claims claims= (Claims) resultMsg1.getData();
		User sysUser = iUserService.getById(claims.getId());
		if (file.isEmpty()) {
			System.out.println("文件为空空");
		}
		String fileName = file.getOriginalFilename(); // 文件名
		String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 后缀名
		fileName =  sysUser.getUname() + UUID.randomUUID() + suffixName; // 新文件名
		File dest = new File(chat +"img/" +fileName);
		if (!dest.getParentFile().exists()) {
			dest.getParentFile().mkdirs();
		}
		try {
			file.transferTo(dest);
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 构建json数据
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("code", "0");
		data.put("msg", "");
		Map<String, String> sourceUrl = new HashMap<String, String>();
		sourceUrl.put("src", chat_url+"img/"+fileName);
		data.put("data", sourceUrl);
		return data;
	}
	/**
	 * 发送图片,上传图片接口
	 */
	@RequestMapping(value = "/file")
	public Object file(@RequestHeader("token") String token,@RequestParam(value = "file") MultipartFile file) {
		ResultMsg resultMsg1= JwtUtils.validateJWT(token);
		if (!resultMsg1.isSuccess()){
			return null;
		}
		Claims claims= (Claims) resultMsg1.getData();
		User sysUser = iUserService.getById(claims.getId());
		if (file.isEmpty()) {
			System.out.println("文件为空空");
		}
		String fileName = file.getOriginalFilename(); // 文件名
		String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 后缀名
		fileName =  sysUser.getUname() + UUID.randomUUID() + suffixName; // 新文件名
		File dest = new File(chat +"file/" +fileName);
		if (!dest.getParentFile().exists()) {
			dest.getParentFile().mkdirs();
		}
		try {
			file.transferTo(dest);
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 构建json数据
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("code", "0");
		data.put("msg", "");
		Map<String, String> sourceUrl = new HashMap<String, String>();
		sourceUrl.put("src", chat_url+"file/"+fileName);
		data.put("data", sourceUrl);
		return data;
	}

}
